import os
import re
import gtk
import gtk.glade
from DlgDirectorySelection import DlgDirectorySelection

class DlgNewPlatform:
    def __init__( self, wizard):
        self.__wiz = wizard
        self.__glade = self.__wiz.getPreferences().getGUIFile( "createplatfwindow")
        self.__xml = gtk.glade.XML( self.__glade)
        if self.__xml:
            self.__win = self.__xml.get_widget( "DlgNewPlatform")
            self.__win.connect( "destroy", self.__onCancel)
            self.__handlers = { "on_btnBrowseProjDir_clicked": self.__onBtnProjDir
                , "on_btnBrowseBuildDir_clicked": self.__onBtnBuildDir
                , "on_btnBrowseCMakeLists_clicked": self.__onBtnCMakeDir
                , "on_btnCancel_clicked": self.__onCancel
                , "on_btnOK_clicked": self.__onOK
                , "on_entryProjName_changed": self.__onProjNameChanged
                , "on_entryProjDir_changed": self.__onProjDirChanged
                , "on_entryBuildDir_changed": self.__onBuildDirChanged
                , "on_entryCMakeLists_changed": self.__onCMakeChanged}
            self.__xml.signal_autoconnect( self.__handlers)
            self.__entryProjName = self.__xml.get_widget( "entryProjName")
            self.__txtviewDescr = self.__xml.get_widget( "textviewDescr")
            self.__bufDescr = self.__txtviewDescr.get_buffer()
            self.__entryProjDir = self.__xml.get_widget( "entryProjDir")
            self.__entryBuildDir = self.__xml.get_widget( "entryBuildDir")
            self.__entryCMake = self.__xml.get_widget( "entryCMakeLists")
            self.__btnOK = self.__xml.get_widget( "btnOK")
            self.__btnOK.set_sensitive( False)
            self.__btnCancel = self.__xml.get_widget( "btnCancel")
            self.__entryDefault = { "bg": "#FFFFFF", "fg": "#000000" }
            self.__entryWrong = { "bg": "#F74646", "fg": "#FFFFFF" }
            self.__entryOK = { "bg": "#8DF793", "fg": "#000000" }

    def show( self):
        self.__win.show_all()

    def __onOK( self, btn):
        if self.__projFileName != "":
            start, end = self.__bufDescr.get_bounds()
            values = {}
            values[ "file"] = self.__projFileName
            values[ "name"] = self.__entryProjName.get_text()
            values[ "descr"] = self.__bufDescr.get_text( start, end)
            values[ "projdir"] = self.__entryProjDir.get_text()
            values[ "builddir"] = self.__entryBuildDir.get_text()
            values[ "cmakelists"] = self.__entryCMake.get_text()
            self.__wiz.saveNewPlatform( values)

        self.__onCancel( btn)

    def __onCancel( self, widget):
        self.__win.destroy()

    def __onBtnProjDir( self, btn):
        fs = DlgDirectorySelection( "Choose the project diretory")
        selecteddir = fs.getDirSelected()
        if len( selecteddir) > 0:
            self.__entryProjDir.set_text( selecteddir)

    def __onBtnBuildDir( self, btn):
        fs = DlgDirectorySelection( "Choose the build diretory")
        selecteddir = fs.getDirSelected()
        if len( selecteddir) > 0:
            self.__entryBuildDir.set_text( selecteddir)
        
    def __onBtnCMakeDir( self, btn):
        fs = DlgDirectorySelection( "Choose the diretory where CMakeLists.txt is")
        selecteddir = fs.getDirSelected()
        if len( selecteddir) > 0:
            self.__entryCMake.set_text( selecteddir)
        
    def __try2EnableOK( self):
        projName = self.__entryProjName.get_text()
        projDir  = self.__entryProjDir.get_text()
        buildDir = self.__entryBuildDir.get_text()
        cmakeDir = self.__entryCMake.get_text()
        self.__projFileName = ""
        if  len( projName) > 0 and len( projDir) > 0 and len( buildDir) > 0 and len( cmakeDir) > 0:
            self.__projFileName = os.path.join( self.__wiz.getPlatformsDir(), projName) + ".cfg"
            if os.path.isfile( self.__projFileName):
                self.__btnOK.set_sensitive( False)
                msg = "The file [" + self.__projFileName + "] already exists!\nTry another project name"
                dlg = gtk.MessageDialog( None, 0, gtk.MESSAGE_ERROR, gtk.BUTTONS_OK, msg)
                dlg.run()
                dlg.destroy()
                self.__projFileName = ""
            else:
                self.__btnOK.set_sensitive( True)
        else:
            self.__btnOK.set_sensitive( False)

    def __onProjNameChanged( self, entry):
        name = entry.get_text()
        if len( name) == 0:
            self.__setEntryColorStatus( entry, self.__entryDefault)
        else:
            m = re.search( "[^\w_-]", name)
            if m:
                self.__setEntryColorStatus( entry, self.__entryWrong)
            else:
                self.__setEntryColorStatus( entry, self.__entryOK)

        self.__try2EnableOK()


    def __validateEntryDir( self, entry):
        txt = entry.get_text()
        if len( txt) == 0:
            self.__setEntryColorStatus( entry, self.__entryDefault)
        else:
            if os.path.isdir( entry.get_text()):
                self.__setEntryColorStatus( entry, self.__entryOK)
            else:
                self.__setEntryColorStatus( entry, self.__entryWrong)

        self.__try2EnableOK()


    def __onProjDirChanged( self, entry):
        self.__validateEntryDir( entry)

    def __onBuildDirChanged( self, entry):
        self.__validateEntryDir( entry)

    def __onCMakeChanged( self, entry):
        self.__validateEntryDir( entry)

    def __setEntryColorStatus( self, entry, status):
        # This does not yield expected results. 
        entry.modify_bg( gtk.STATE_NORMAL, gtk.gdk.color_parse( status[ "bg"]))
        entry.modify_fg( gtk.STATE_NORMAL, gtk.gdk.color_parse( status[ "fg"]))

